package com.minivision.sdktestdemo.controler.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;
import com.minivision.sdktestdemo.ui.view.RectangleColorPicker;

import butterknife.Bind;
import butterknife.BindDimen;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/6/15.
 */
public class ThemeColorControler {
    private Activity mContext;

    public ThemeColorControler(Activity activity) {
        mContext = activity;
        ButterKnife.bind(this, activity);
        mToolbar.setNavigationIcon(R.drawable.ic_action_arrow_left);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.onBackPressed();
            }
        });
        init();
    }

    @Bind(R.id.tool_bar)
    Toolbar mToolbar;

    @OnClick(R.id.skin_red)
    void onSelectRedSkin() {
        setTheme(ThemeUtils.RED_THEME);
        ThemeUtils.saveTheme(mContext);
        ThemeUtils.changeTheme(mContext, R.id.layout_main);
    }

    @OnClick(R.id.skin_white)
    void onSelectWhiteSkin() {
        setTheme(ThemeUtils.WHITE_THEME);
        ThemeUtils.saveTheme(mContext);
        ThemeUtils.changeTheme(mContext, R.id.layout_main);
    }

    private AlertDialog mDlg;

    private View mPickerContentView;

    @OnClick(R.id.skin_diy)
    void onSelectDiySkin() {
        if (mDlg == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请选择颜色");
            mPickerContentView = LayoutInflater.from(mContext).inflate(R.layout.layout_color_picker, null);
            builder.setView(mPickerContentView);
            builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mDlg.dismiss();
                    RectangleColorPicker picker = (RectangleColorPicker) mPickerContentView.findViewById(R.id.color_picker);
                    ThemeUtils.setThemeColor(picker.getSelectedColor());
                    ThemeUtils.setThemeType(ThemeUtils.DIY_THEME);
                    ThemeUtils.saveTheme(mContext);
                    ThemeUtils.changeTheme(mContext, R.id.layout_main);
                }
            });
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mDlg.dismiss();
                }
            });
            mDlg = builder.create();
        }
        mDlg.show();
    }

    public void init() {
        setTheme(ThemeUtils.getThemeType());
    }

    @Bind(R.id.red_using)
    TextView redThemeText;

    @Bind(R.id.red_icon_selected)
    ImageView redIcon;

    @Bind(R.id.white_using)
    TextView whiteThemeText;

    @Bind(R.id.white_icon_selected)
    ImageView whiteIcon;

    @Bind(R.id.diy_using)
    TextView diyThemeText;

    @Bind(R.id.diy_icon_selected)
    ImageView diyIcon;

    public void reset() {
        redThemeText.setVisibility(View.GONE);
        redIcon.setVisibility(View.GONE);
        whiteThemeText.setVisibility(View.GONE);
        whiteIcon.setVisibility(View.GONE);
        diyThemeText.setVisibility(View.GONE);
        diyIcon.setVisibility(View.GONE);
    }

    public void setTheme(int type) {
        reset();
        switch (type) {
            case ThemeUtils.RED_THEME:
                redThemeText.setVisibility(View.VISIBLE);
                redIcon.setVisibility(View.VISIBLE);
                break;
            case ThemeUtils.WHITE_THEME:
                whiteThemeText.setVisibility(View.VISIBLE);
                whiteIcon.setVisibility(View.VISIBLE);
                break;
            case ThemeUtils.DIY_THEME:
                diyThemeText.setVisibility(View.VISIBLE);
                diyIcon.setVisibility(View.VISIBLE);
                break;
        }
        ThemeUtils.setThemeType(type);
    }
}
